import numpy as np
123)
np.random.seed(=np.random.randn(8,10) Arr
금융공학프로그래밍3, Quiz5
Problem 1
- Create a 2-dimensional ndarray object ‘Arr’ with the code below and answer the following.
- Write an expression to calculate column sum of Arr.
sum(Arr,axis=0) np.
array([-3.68648899, -3.20797062, 1.89628963, -4.31697028, -1.68652947,
3.31014718, 3.61439765, 2.82609217, 1.68894737, 0.84241575])
- Write an expression that finds the position (row and column index) of an element greater than 2 in Arr
>2) np.where(Arr
(array([1, 1, 4, 4]), array([6, 7, 6, 9]))
Problem 2
Write 3 different expressions to create a pandas Series object ‘S’ displayed as follows.
>>> S
a 0
b 1
c 2
d 3
dtype: int64
import pandas as pd
=pd.Series([0,1,2,3],index=['a','b','c','d'])
S S
a 0
b 1
c 2
d 3
dtype: int64
=pd.Series(dict(a=0,b=1,c=2,d=3))
S S
a 0
b 1
c 2
d 3
dtype: int64
=pd.Series(np.arange(4),index=['a','b','c','d'])
S S
a 0
b 1
c 2
d 3
dtype: int64
Problem 3
- Write 4 different expressions to select the 2nd and the 3rd elements of Snew.
= pd.Series({'a': 1, 'b':4, 'c':2, 'd':3} ) Snew
1:3] Snew[
b 4
c 2
dtype: int64
1,2]] Snew.iloc[[
b 4
c 2
dtype: int64
'b':'c'] Snew[
b 4
c 2
dtype: int64
'b','c']] Snew[[
b 4
c 2
dtype: int64
Problem 4~5
123)
np.random.seed(= pd.DataFrame(np.random.randn(6,7),
DF =list('abcdefg'),
columns=[3,2,4,5,1,0])
index DF
a | b | c | d | e | f | g | |
---|---|---|---|---|---|---|---|
3 | -1.085631 | 0.997345 | 0.282978 | -1.506295 | -0.578600 | 1.651437 | -2.426679 |
2 | -0.428913 | 1.265936 | -0.866740 | -0.678886 | -0.094709 | 1.491390 | -0.638902 |
4 | -0.443982 | -0.434351 | 2.205930 | 2.186786 | 1.004054 | 0.386186 | 0.737369 |
5 | 1.490732 | -0.935834 | 1.175829 | -1.253881 | -0.637752 | 0.907105 | -1.428681 |
1 | -0.140069 | -0.861755 | -0.255619 | -2.798589 | -1.771533 | -0.699877 | 0.927462 |
0 | -0.173636 | 0.002846 | 0.688223 | -0.879536 | 0.283627 | -0.805367 | -1.727669 |
- Fill in the appropriate expression in square brackets so you can select columns ‘c’ and ‘e’.
2:5:2] DF.iloc[:,
c | e | |
---|---|---|
3 | 0.282978 | -0.578600 |
2 | -0.866740 | -0.094709 |
4 | 2.205930 | 1.004054 |
5 | 1.175829 | -0.637752 |
1 | -0.255619 | -1.771533 |
0 | 0.688223 | 0.283627 |
'c','e']] DF[[
c | e | |
---|---|---|
3 | 0.282978 | -0.578600 |
2 | -0.866740 | -0.094709 |
4 | 2.205930 | 1.004054 |
5 | 1.175829 | -0.637752 |
1 | -0.255619 | -1.771533 |
0 | 0.688223 | 0.283627 |
- Fill in the appropriate expression in square brackets so that you can select rows with negative values for column ‘c’.
<0,:] DF.loc[DF.c
a | b | c | d | e | f | g | |
---|---|---|---|---|---|---|---|
2 | -0.428913 | 1.265936 | -0.866740 | -0.678886 | -0.094709 | 1.491390 | -0.638902 |
1 | -0.140069 | -0.861755 | -0.255619 | -2.798589 | -1.771533 | -0.699877 | 0.927462 |
<0] DF[DF.c
a | b | c | d | e | f | g | |
---|---|---|---|---|---|---|---|
2 | -0.428913 | 1.265936 | -0.866740 | -0.678886 | -0.094709 | 1.491390 | -0.638902 |
1 | -0.140069 | -0.861755 | -0.255619 | -2.798589 | -1.771533 | -0.699877 | 0.927462 |